LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2014, 01:23 PM   #16
es131245
LQ Newbie
 
Registered: Mar 2014
Posts: 19

Rep: Reputation: Disabled

awk -version
awk version 20121220 (FreeBSD)

strange. System is updated. So its a third side soft
 
Old 05-11-2016, 07:29 AM   #17
mattthumper
LQ Newbie
 
Registered: Jan 2016
Posts: 19

Rep: Reputation: Disabled
SPF - Thanks!

Quote:
Originally Posted by SPF View Post
What if your string is:
48656C6C6F2074686572652021

And you don't have perl, python, xxd or uni2ascii?
And you are not allowed to install it.

How can you convert it?

Or course I could always write a loop and read it in pairs:

Code:
#!/bin/bash
function hex2string () {
  I=0
  while [ $I -lt ${#1} ];
  do
    echo -en "\x"${1:$I:2}
    let "I += 2"
  done
}
hex2string "48656C6C6F2074686572652021"
SPF, almost 3 years to the day later, your reply/post is just what I needed!
Thanks, Matt
 
Old 05-08-2018, 09:55 PM   #18
thedonco
LQ Newbie
 
Registered: May 2018
Posts: 1

Rep: Reputation: Disabled
Post

I was trying to easily convert back and forth between C-syntax Hex and Ascii (used a lot in Arduino, because apparently that's a fun way to store web pages on embedded controllers), and stumbled upon this thread. It was useful to completing my task, so thought it worth giving back. If nothing else, when I need to do this for something else and misplace these little utils, perhaps my web search will bring me here.

The following scriptlettes should be rather light weight and only use fairly standard POSIX utils (od, sed, bash).

$ cat asc2hex
Code:
#!/bin/bash

while read line; do
        echo $line|od -An -tx1|sed 's/ /, 0x/g;s/^, //;s/$/,/'
done < "${1:-/dev/stdin}"

exit 0
$ cat hex2asc
Code:
#!/bin/bash

while read line; do
        echo -en `echo $line|sed 's/, 0x/\\\\x/g;s/^0x/\\\\x/;s/,$//'`
done < "${1:-/dev/stdin}"

exit 0
Example usage:
Code:
$ echo "Hello World!"|./asc2hex
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x0a,

$ echo "Hello World!"|./asc2hex|./hex2asc
Hello World!
Alternatively, scriptlettes will also accept direct file as an option:
Code:
$ cat hello_world
Hello World!

$ ./asc2hex hello_world
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x0a,

$ ./asc2hex hello_world|./hex2asc
Hello World!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting extended ascii (ë,ô) in bash script Hko Programming 4 12-29-2012 03:42 AM
How do i write to a serial port (modem) in ascii or hex directly? Taliesin.duchaos Linux - Networking 6 04-21-2006 08:31 AM
Hex socks Linux - General 4 02-17-2005 12:05 PM
bash printing extended ASCII characters nutthick Programming 6 02-04-2005 02:15 PM
Binary to Hex Ascii converter carboncopy Slackware 1 05-28-2004 09:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration